package com.woniuxy.creditsystem.systemmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.systemmag.service.IRoleMenuService;
import com.woniuxy.creditsystem.systemmag.service.IRoleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色的增、删、查、改
 * @author hejie
 */
@RequestMapping("/role")
@RestController
public class RoleController {

    @Resource
    private IRoleService roleServiceImpl;
    @Resource
    private IRoleMenuService roleMenuServiceImpl;
    //从application.properties中获取poi的excel文件存放路径
    @Value("${poiFilePath}")
    private String poiFilePath;

    /**
     * 把前台传回来的table中的RoleInfo list集合 导出到一个Excel文件
     * @author hejie
     * @param tableDataBean= tableDataJson(前端表格数据)+ simpleFileNameUserSet（用户定义的表格名字）
     * @return ResultMsg
     */
    @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public ResultMsg tableDataJson(@RequestBody TableDataBean<RoleInfo> tableDataBean ) throws IOException {
        List<RoleInfo> tableDataJson=tableDataBean.getTableDataJson();
        String simpleFileNameUserSet=tableDataBean.getSimpleFileNameUserSet();

        roleServiceImpl.write2excel(tableDataJson,poiFilePath,simpleFileNameUserSet);

        System.out.println("\n"+TraceUtil.positionTrace());
        for (RoleInfo roleInfo:tableDataJson) {
            System.out.println(roleInfo);
        }
        ResultMsg resultMsg = new ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }

    /**
     * 新增角色
     * @author hejie
     * @param roleInfo
     * @return
     */
    @PostMapping(value="/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveRole(RoleInfo roleInfo,String menuPermissionArray){
        System.out.println("\nmenuPermissionArray=***"+menuPermissionArray+"\tRoleInfo="+roleInfo+TraceUtil.positionTrace());
        boolean re = menuPermissionArray.contains("");
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        roleServiceImpl.saveRoleInfo(roleInfo);

        //传入roleInfo.id  和menuIds  （要用list，在sql中）
        roleMenuServiceImpl.saveRoleMenuBean(roleInfo.getId(),menuPermissionArray);
        return resultMsg;
    }

    /**
     * 修改角色
     * @author hejie
     * @param roleInfo 被编辑的RoleInfo对象
     * @param menuPermissionArray 前端穿回来的 表示该角色能看到的菜单：如1,3,1,4,1,5,1,8,others
     *                              “1,3”是 账号管理；“1,4”是 角色管理；“1,5”是 系统日志；“others”是其他菜单-一般都具有others的访问权限；
     * @return
     */
    @PutMapping(value="/edit",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateRole(RoleInfo roleInfo,String menuPermissionArray){

        System.out.println("\nroleInfo="+roleInfo+"\tmenuPermisssionArray="+menuPermissionArray+TraceUtil.positionTrace());


        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        //更新role_info表
        roleServiceImpl.updateRoleInfo(roleInfo);

        //更新role_menu_info表
        //普通更新状态，不会执行以下代码
        if (menuPermissionArray!=null){
            //传入roleInfo.id  和menuIds  （要用list，在sql中）
            roleMenuServiceImpl.updateRoleMenuBean(roleInfo.getId(),menuPermissionArray);
        }
        return resultMsg;
    }

    /**
     * 修改角色 的状态
     * @author hejie
     * @param roleInfo
     * @param id
     * @return
     */
    @RequestMapping(value="/status/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateRoleStatus(RoleInfo roleInfo, @PathVariable("id") Long id ){
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        //更新role_info表
        roleServiceImpl.updateRoleInfo(roleInfo);
        return resultMsg;
    }

    /**
     * 查询所有的 角色（用来做selection的option）
     * @author hejie
     * @return
     */
    @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public List<RoleInfo> findAll(){
        System.out.println("\n in role-findAll"+ TraceUtil.positionTrace());
        List<RoleInfo> roleInfoList=roleServiceImpl.findAll();
        for (RoleInfo r:roleInfoList ) {
            System.out.println(r);
        }
        return roleInfoList;
    }

    /**
     * 分页查询数据
     * @author hejie
     * @param roleInfo 查询数据
     * @return
     */
    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public PageInfo<RoleInfo> findAllByParams(int pageNum, RoleInfo roleInfo){
        System.out.println("\ninfindAllByParams..."+roleInfo);

        int pageSize=10;
        PageInfo<RoleInfo> pageInfo = roleServiceImpl.findAllByParams(pageNum,pageSize,roleInfo);
        System.out.println("\npageInfo.getList()="+pageInfo.getList()+TraceUtil.positionTrace());
        return pageInfo;
    }

    /**
     * 删除一些RoleInfo
     * @author hejie
     * @param ids
     * @return
     */
   /* @RequestMapping(value="/{ids}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(@PathVariable("ids") Long[] ids){
        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);

        for (Long id:ids) {
            roleServiceImpl.deleteRoleInfo(id);
        }
        return resultMsg;
    }*/
}
